﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NGourd.Core.Features;

namespace NGourd.Core.Runners
{
    class FeatureLibrary
    {
        private readonly IDictionary<string, Feature> Features
            = new Dictionary<string, Feature>(StringComparer.InvariantCultureIgnoreCase);

        public void AddFeatureType(Type featureType)
        {
            var feature = CreateFeatureFromType(featureType);
            AddFeature(feature);
        }

        public void AddFeature(Feature feature)
        {
            if (Features.ContainsKey(feature.Name))
                GetFeature(feature.Name).Merge(feature);
            else
                Features.Add(feature.Name, feature);
        }

        private Feature CreateFeatureFromType(Type featureType)
        {
            if (typeof(Feature).IsAssignableFrom(featureType))
                return Activator.CreateInstance(featureType) as Feature;
            throw new ArgumentException("featureType must inherit from feature", "featureType");
        }

        public Feature GetFeature(string name)
        {
            return Features[name];
        }

        public IEnumerable<string> DefinedFeatures
        {
            get
            {
                return Features.Keys;
            }
        }
    }
}
